<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-01-06 10:18:42
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-06 11:14:30
-->
<template>
  <div>
    <el-dialog
      title="新建项目"
      :visible.sync="dialog.newProjectShow"
      width="50%"
      @close="projectClose"
    >
      <el-form
        :model="projectForm"
        ref="projectForm"
        :rules="projectRules"
        label-position="top"
        :inline="false"
        size="normal"
      >
        <div class="formTop">
          <el-form-item label="项目" prop="name">
            <el-input
              v-model="projectForm.name"
              placeholder="请输入项目名称"
              style="width: 330px"
            ></el-input>
          </el-form-item>

          <el-form-item label="负责人" prop="userId">
            <el-select
              v-model="projectForm.userId"
              placeholder="请选择负责人"
              clearable
              filterable
              class="chaSel"
              style="width: 330px"
            >
              <el-option
                v-for="item in chargerOptions"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="成员">
            <el-select
              v-model="projectForm.userIds"
              placeholder="请选择成员"
              clearable
              filterable
              multiple
              class="chaSel"
              style="width: 330px"
            >
              <el-option
                v-for="item in chargerOptions"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="优先级">
            <el-select
              v-model="projectForm.level"
              placeholder="请选择优先级"
              style="width: 330px"
            >
              <el-option
                v-for="item in priorityOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <el-form-item label="计划时间段">
          <el-date-picker
            v-model="projectForm.timeSlot"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="projectForm.remark"
            placeholder="请输入备注"
            size="normal"
            type="textarea"
            rows="4"
            style="width: 600px"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="dialog.newProjectShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { editProject } from "@/api/task/projectManagement";
import { distriBution } from "@/api/registration";
export default {
  props: {
    dialog: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      projectForm: {
        id: null,
        name: null,
        userId: null,
        userIds: [],
        level: null,
        timeSlot: null,
        remark: null,
      },
      projectRules: {
        name: [
          {
            required: true,
            message: "请输入项目名称",
            trigger: "blur",
          },
        ],
        userId: [
          {
            required: true,
            message: "请选择负责人",
            trigger: "change",
          },
        ],
      },
      //   负责人
      chargerOptions: [],
      //   成员
      memberOptions: [],
      //   优先级
      priorityOptions: [
        {
          value: 1,
          label: "高",
        },
        {
          value: 2,
          label: "中",
        },
        { value: 3, label: "低" },
      ],
    };
  },
  created() {
    this.getChargeList();
  },
  methods: {
    //获取负责人
    getChargeList() {
      distriBution().then((res) => {
        this.chargerOptions = res.data;
      });
    },
    //   关闭新建项目
    projectClose() {
      this.projectForm = {
        id: null,
        name: null,
        userId: null,
        userIds: [],
        level: null,
        timeSlot: null,
        remark: null,
      };
      this.dialog.newProjectShowEject = false;
    },
    // 保存项目
    onSubmit() {
      this.$refs.projectForm.validate((valid) => {
        if (!valid) return;
        editProject(this.projectForm).then((res) => {
          this.$message.success(res.msg);
          this.dialog.newProjectShow = false;
          this.$parent.getList();
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.formTop {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  .el-form-item {
    width: 50%;
  }
}
</style>